package com.sync.test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel.MapMode;

public class ByteVsMapBuffer {
	public static void main(String[] args) {
		byte[] a = new byte[1024];
		MappedByteBuffer mappedByteBuffer = null;
		a = new byte[1024];
		long start = System.currentTimeMillis();
		for (int i = 0; i < 10000000; ++i) {
			a = new byte[1024];
		}
		for (int i = 0; i < 70000000; ++i) {
			a[i % 1024] = (byte) i;
		}
		System.err.println(System.currentTimeMillis() - start);
		start = System.currentTimeMillis();
		try {
			mappedByteBuffer = new RandomAccessFile("D:/b.txt", "rw")
					.getChannel().map(MapMode.READ_WRITE, 0, 1024 * 1024);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for (int i = 0; i < 70000000; ++i) {
			mappedByteBuffer.put(i % 1024, (byte) i);
		}
		System.err.println(System.currentTimeMillis() - start);
	}
}
